﻿package mortal.game.scene3D.ai.singleAIs.autofight
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.systemSetting.*;

    public class AutoFightPickUpItemAI extends Object
    {
        private var _timer:Timer;
        private var _pickedDic:Dictionary;
        private var _curDis:int = -1;
        private var _moveToItemEnd:Boolean = true;
        private var _lastPickupItem:ItemPlayer;
        private var _lastMsgTime:int = -1;
        public static var hasDrop:Boolean = false;

        public function AutoFightPickUpItemAI()
        {
            this._pickedDic = new Dictionary();
            return;
        }// end function

        public function start() : void
        {
            this._lastPickupItem = null;
            this._moveToItemEnd = true;
            hasDrop = false;
            AutoFightAI.isMoveToPickupItem = false;
            if (this._timer == null)
            {
                this._timer = new Timer(GameConst.PickUpTimeDistance);
                this._timer.addEventListener(TimerEvent.TIMER, this.timerHandler);
            }
            this._timer.start();
            this.timerHandler(null);
            return;
        }// end function

        public function stop() : void
        {
            this._lastPickupItem = null;
            this._timer.stop();
            hasDrop = false;
            this._moveToItemEnd = true;
            AutoFightAI.isMoveToPickupItem = false;
            return;
        }// end function

        private function pickUpItem(param1:ItemPlayer, param2:SPlayerItem, param3:Boolean = false) : ItemPlayer
        {
            var _loc_6:int = 0;
            if (!param3 && !ItemsUtil.isDefenseEquip(new ItemData(param2.itemCode)) && Cache.instance.pack.backPackCache.isPackFull(param2.itemCode, 1))
            {
                this._curDis = int.MAX_VALUE;
                _loc_6 = FrameManager.flashFrame.getTime();
                if (_loc_6 - this._lastMsgTime > 5000)
                {
                    this._lastMsgTime = _loc_6;
                    MsgManager.showRollTipsMsg(Language.getString(20608));
                }
                return param1;
            }
            if (this._lastPickupItem == param1)
            {
                return param1;
            }
            this._curDis = GeomUtil.calcDistance(param1.x2d, param1.y2d, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            if (this._curDis > GameConst.PickUpDistance)
            {
                return param1;
            }
            var _loc_4:* = int(this._pickedDic[param1.dropEntityInfo.entityId]);
            if (_loc_4 <= 2)
            {
                hasDrop = true;
            }
            var _loc_5:* = param1.dropEntityInfo.playerItems[0];
            if (_loc_4 <= 2)
            {
                _loc_4++;
                this._pickedDic[param1.dropEntityInfo.entityId] = _loc_4;
                if (_loc_5 != null)
                {
                    GuidePickupItemFly.instance.inCode(_loc_5.itemCode, param1);
                }
                GameProxy.sceneProxy.pickup(param1.dropEntityInfo.entityId, _loc_5);
                this._lastPickupItem = param1;
                hasDrop = true;
                return null;
            }
            else
            {
                this._curDis = 10000;
            }
            return param1;
        }// end function

        private function checkAndFixPickedDic() : void
        {
            var _loc_2:* = undefined;
            var _loc_1:int = 0;
            for (_loc_2 in this._pickedDic)
            {
                
                _loc_1++;
            }
            if (_loc_1 >= 15)
            {
                this._pickedDic = new Dictionary();
            }
            return;
        }// end function

        private function timerHandler(event:TimerEvent) : void
        {
            var _loc_5:ItemPlayer = null;
            var _loc_6:ItemPlayer = null;
            var _loc_9:ItemPlayer = null;
            var _loc_10:SPlayerItem = null;
            var _loc_11:ItemInfo = null;
            var _loc_12:int = 0;
            var _loc_13:int = 0;
            var _loc_14:AutoFightAI = null;
            var _loc_2:* = !ClientSetting.local.getIsDone(IsDoneType.PickUpItem);
            var _loc_3:* = !ClientSetting.local.getIsDone(IsDoneType.PickUpEquip);
            var _loc_4:* = !ClientSetting.local.getIsDone(IsDoneType.PickUpDrug);
            hasDrop = false;
            if (this._moveToItemEnd)
            {
                AutoFightAI.isMoveToPickupItem = false;
            }
            if (!_loc_2 && !_loc_3 && !_loc_4)
            {
                return;
            }
            this.checkAndFixPickedDic();
            var _loc_7:* = ThingUtil.dropItemUtil.dropItemMap.map;
            var _loc_8:* = int.MAX_VALUE;
            for each (_loc_9 in _loc_7)
            {
                
                _loc_10 = _loc_9.dropEntityInfo.playerItems[0] as SPlayerItem;
                if (!EntityUtil.isSelf(_loc_9.dropEntityInfo.ownerEntityId) && GameMapUtil.curMapState.isHoleBoss == false && GameMapUtil.curMapState.isGuildBoss == false && !CopyUtil.isInTeamDefenseCopy && !CopyUtil.isInDefenseCopy)
                {
                    continue;
                }
                if (_loc_10 == null)
                {
                    _loc_6 = this.pickUpItem(_loc_9, _loc_10, true);
                    if (_loc_6 == null)
                    {
                        return;
                    }
                    if (this._curDis < _loc_8)
                    {
                        _loc_8 = this._curDis;
                        _loc_5 = _loc_6;
                    }
                    continue;
                }
                _loc_11 = ItemConfig.instance.getConfig(_loc_10.itemCode);
                if (ItemsUtil.isEquipByItemInfo(_loc_11))
                {
                    if (_loc_3)
                    {
                        _loc_12 = Cache.instance.autoFight.getPickUpEquipMinColor();
                        _loc_13 = Cache.instance.autoFight.getPickUpEquipCareer();
                        if (_loc_11.color >= _loc_12 && (_loc_13 <= 0 || CareerUtil.isCareerSuit(_loc_11.career, _loc_13)))
                        {
                            _loc_6 = this.pickUpItem(_loc_9, _loc_10);
                            if (_loc_6 == null)
                            {
                                return;
                            }
                            if (this._curDis < _loc_8)
                            {
                                _loc_8 = this._curDis;
                                _loc_5 = _loc_6;
                            }
                        }
                    }
                    continue;
                }
                if (ItemsUtil.isDrugByItemInfo(_loc_11))
                {
                    if (_loc_4 || _loc_11.effect == 1)
                    {
                        _loc_6 = this.pickUpItem(_loc_9, _loc_10);
                        if (_loc_6 == null)
                        {
                            return;
                        }
                        if (this._curDis < _loc_8)
                        {
                            _loc_8 = this._curDis;
                            _loc_5 = _loc_6;
                        }
                    }
                    continue;
                }
                if (_loc_2)
                {
                    _loc_6 = this.pickUpItem(_loc_9, _loc_10);
                    if (_loc_6 == null)
                    {
                        return;
                    }
                    if (this._curDis < _loc_8)
                    {
                        _loc_8 = this._curDis;
                        _loc_5 = _loc_6;
                    }
                }
            }
            this._lastPickupItem = null;
            if (_loc_5 != null && _loc_8 < GameConst.PickUpMoveDistance)
            {
                _loc_14 = AIManager.curAI as AutoFightAI;
                if (_loc_14 != null)
                {
                    AutoFightAI.isMoveToPickupItem = true;
                    hasDrop = true;
                    this._moveToItemEnd = false;
                    _loc_14.cancelAll();
                    _loc_14.addMoveTo(new Point(_loc_5.x2d, _loc_5.y2d), 50);
                    _loc_14.addCallback(this.movedEnd);
                    _loc_14.startAIList();
                }
            }
            return;
        }// end function

        private function movedEnd(param1 = null) : void
        {
            this._moveToItemEnd = true;
            return;
        }// end function

    }
}
